/**
 * 基站管理初始化
 */
var Station = {
    id: "StationTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Station.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '地市', field: 'cityName', align: 'center', valign: 'middle'},
        {title: '区县', field: 'districtName', align: 'center', valign: 'middle'},
        {title: '区域名称', field: 'areaName', align: 'center', valign: 'middle'},
        {title: '子区域名称', field: 'subAreaName', align: 'center', valign: 'middle'},
        {title: 'lac', field: 'lac', align: 'center', valign: 'middle'},
        {title: 'ci', field: 'ci', align: 'center', valign: 'middle'},
        {title: '小区名称', field: 'cellName', align: 'center', valign: 'middle'},
        {title: '经度', field: 'lon', align: 'center', valign: 'middle'},
        {title: '纬度', field: 'lat', align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Station.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Station.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加基站
 */
Station.openAddStation = function () {
    var index = layer.open({
        type: 2,
        title: '添加基站',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/station/station_add'
    });
    this.layerIndex = index;
};

/**
 * 打开修改基站管理
 */
Station.openUpdateStation = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '基站详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/station/station_update/' + Station.seItem.lac + '/' + Station.seItem.ci
        });
        this.layerIndex = index;
    }
};

/**
 * 删除基站
 */
Station.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/station/delete", function (data) {
            Feng.success("删除成功!");
            Station.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("lac",this.seItem.lac);
        ajax.set("ci",this.seItem.ci);
        ajax.start();
    }
};

/**
 * 查询区域基站
 */
/*Station.areaStationSearch = function () {
    var queryData = {};
    queryData['isAreaCell'] = "1";
    Station.table.refresh({query: queryData});
};*/

/**
 * 下载区域基站
 */
Station.areaStationDownload = function () {
    // var ajax = new $axSyn(Feng.ctxPath + "/station/areaStationDownload", 
    // function (data) {
    //     layer.load(1, {shade: false});
    // },
    // function (data) {
    //     Station.table.refresh();
    //     layer.closeAll("loading");
    //     Feng.success("下载成功!");
    // }, function (data) {
    //     layer.closeAll("loading");
    //     Feng.error("下载失败!" + data.responseJSON.message + "!");
    // });
    // ajax.start();

    var url = Feng.ctxPath+"/station/areaStationDownload";
    var fileName = "lacci.txt";
    var form = $("<form></form>").attr("action", url).attr("method", "post");
    form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", fileName));
    form.appendTo('body').submit().remove();
};

/**
 * 查询基站列表
 */
Station.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    queryData['city'] = $("#city").val();
    queryData['county'] = $("#county").val();
    queryData['isAreaCell'] = "";
//    queryData['city'] = "";
//    queryData['county'] = "";
    Station.table.refresh({query: queryData});
};

Station.citySearch = function () {
    var queryData = {};
    queryData['city'] = $("#city").val();
    queryData['isAreaCell'] = "";
    queryData['county'] = "";
    queryData['condition'] = "";
    Station.table.refresh({query: queryData});
};

Station.countySearch = function () {
    var queryData = {};
    queryData['county'] = $("#county").val();
    queryData['isAreaCell'] = "";
    queryData['city'] = "";
    queryData['condition'] = "";
    Station.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Station.initColumn();
    var table = new BSTable(Station.id, "/station/list", defaultColunms);
    table.setPaginationType("client");
    Station.table = table.init();
});
